package com.fenghuo.map;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fenghuo.model.Histroy;
import com.fenghuo.myviews.HistroyScrollView;
import com.fenghuo.utils.Values;

import net.tsz.afinal.FinalDb;

import java.util.List;

public class Ac_History extends Activity implements HistroyScrollView.OnContentClickListener, OnClickListener {

    private HistroyScrollView histroyScrollView;
    private LinearLayout layout;
    private List<Histroy> list;
    private FinalDb finalDb;
    private TextView tv_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_history);

        histroyScrollView = (HistroyScrollView) findViewById(R.id.histroyScrollView1);
        layout = (LinearLayout) findViewById(R.id.linea_nohistory);
        tv_back = (TextView) findViewById(R.id.tv_back_history);
        tv_back.setOnClickListener(this);

        finalDb = FinalDb.create(Ac_History.this, Values.DB_NAME);
        list = finalDb.findAll(Histroy.class, "id desc");
        if (list != null && list.size() > 0) {
            histroyScrollView.setData(list);
            histroyScrollView.setOnContenClickListener(this);
            histroyScrollView.setVisibility(View.VISIBLE);
            layout.setVisibility(View.GONE);
        }
    }

    @Override
    public void onclick(final int posistion) {
        new AlertDialog.Builder(Ac_History.this)
                .setTitle("删除?")
                .setMessage("操作将不可恢复")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {

                        finalDb.delete(list.get(posistion));
                        list.remove(posistion);
                        if (list.size() < 1) {
                            histroyScrollView.setVisibility(View.GONE);
                            layout.setVisibility(View.VISIBLE);
                            return;
                        }
                        histroyScrollView.setData(list);
                        histroyScrollView.invalidate();
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back_history:
                finish();
                break;

            default:
                break;
        }
    }

}
